package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;
    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询课程里面的所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2.根据课程id查询所有的小节
        List<EduVideo> eduVideoList = videoService.selectList(courseId);
//        for(EduVideo eduVideo : eduVideoList) {
//            System.out.println(eduVideo);
//        }
        //创建list集合，最终数据
        List<ChapterVo> res = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        //3.遍历查询章节list集合进行封装
        int i = 0;
        for(EduChapter eduChapter : eduChapterList) {
            //System.out.println(eduChapter);
            ChapterVo temp = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, temp);
            res.add(temp);
            map.put(eduChapter.getId(), i);
            i += 1;
        }
        //System.out.println("======================");
        //System.out.println(map);
        //4.遍历查询小节list集合，进行封装
        for(EduVideo eduVideo : eduVideoList) {
            VideoVo temp = new VideoVo();
            BeanUtils.copyProperties(eduVideo, temp);
            //System.out.println(temp);
            res.get(map.get(eduVideo.getChapterId())).getChildren().add(temp);
        }
        return res;
    }

    //删除章节方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //看看有没有小节 去查询小节表，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        if(videoService.count(wrapper) == 0) {
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        } else {//查询出数据
            throw new GuliException(20001, "不能删除");
        }
    }

    @Override
    public void removeChapter(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        baseMapper.delete(wrapper);
    }
}
